import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    goodsItme: [],
    goods: []
  },
  mutations: {
    GOODS_DATA(state, item) {
      state.goods = item;
      state.goodsItme = item
    },
    ADD_DATA(state, item) {
      state.goods.unshift(item);
    },
    SEARCH_DATA(state, value) {
      state.goods = [];
      state.goodsItme.forEach(ele => {
        if (ele.name.includes(value)) {
          state.goods.push(ele)
        }
      })
    }
  },
  actions: {
    GOODS_DATA(context) {
      axios
        .get("/shopList.json")
        .then(res => {
          context.commit('GOODS_DATA', res.data);
        })
        .catch(err => {
          console.log(err);
        });
    },
    ADD_DATA(context, item) {
      context.commit('ADD_DATA', item)
    },
    SEARCH_DATA(context, value) {
      context.commit('SEARCH_DATA', value)
    }

  },
  modules: {}
})